package com.daxidi.cn.controller;


import com.daxidi.cn.entity.ShopType;
import com.daxidi.cn.service.IShopTypeService;
import com.daxidi.cn.utils.ReturnResult;
import com.daxidi.cn.utils.ReturnResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商家类型 前端控制器
 * </p>
 *
 * @author wsh
 * @since 2020-11-09
 */
@Log4j
@Api(tags = "商家类型模块")
@RestController
@RequestMapping("/api/v1/union/shopType")
public class ShopTypeController {
    @Autowired
    private IShopTypeService shopTypeService;

    @ApiOperation(value = "商家类型模块  添加接口")
    @ApiResponses({
            @ApiResponse(code = 140, message = "添加成功"),
            @ApiResponse(code = 141, message = "添加失败"),
            @ApiResponse(code = 401, message = "未经授权"),
            @ApiResponse(code = 403, message = "被禁止"),
            @ApiResponse(code = 404, message = "请求路径没有货页面跳转路径不对")
    })
    @PostMapping("/")
    public ReturnResult addShopType(@RequestBody ShopType shopType){
        try {
            shopType.setCreatedAt(new Date());
            shopType.setUpdatedAt(null);
            shopType.setDeletedAt(null);
            shopTypeService.addShopType(shopType);
            return ReturnResultUtils.returnSuccess();
        } catch (Exception e) {
            log.error("添加失败" + e);
            return ReturnResultUtils.returnFailMsg(141, "添加失败");
        }
    }

    @ApiOperation(value = "商家类型模块  删除接口")
    @ApiResponses({
            @ApiResponse(code = 142, message = "删除成功"),
            @ApiResponse(code = 143, message = "删除失败"),
            @ApiResponse(code = 401, message = "未经授权"),
            @ApiResponse(code = 403, message = "被禁止"),
            @ApiResponse(code = 404, message = "请求路径没有货页面跳转路径不对")
    })
    @DeleteMapping("/{id}")
    public ReturnResult deleteShopTypeById(@PathVariable("id") Integer id){
        try {
            shopTypeService.deleteShopTypeById(id);
            return ReturnResultUtils.returnSuccess();
        } catch (Exception e) {
            log.error("删除失败" + e);
            return ReturnResultUtils.returnFailMsg(143, "删除失败");
        }
    }

    @ApiOperation(value = "商家类型模块  修改接口")
    @ApiResponses({
            @ApiResponse(code = 144, message = "修改成功"),
            @ApiResponse(code = 145, message = "修改失败"),
            @ApiResponse(code = 401, message = "未经授权"),
            @ApiResponse(code = 403, message = "被禁止"),
            @ApiResponse(code = 404, message = "请求路径没有货页面跳转路径不对")
    })
    @PutMapping("/")
    public ReturnResult updateShopTypeById(@RequestBody ShopType shopType){
        try {
            ShopType shopType1 = shopTypeService.selectShopType(shopType.getId());
            shopType.setDeletedAt(null);
            shopType.setUpdatedAt(new Date());
            shopType.setCreatedAt(shopType1.getCreatedAt());

            shopTypeService.updateShopTypeById(shopType);
            return ReturnResultUtils.returnSuccess();
        } catch (Exception e) {
            log.error("修改失败" + e);
            return ReturnResultUtils.returnFailMsg(145, "修改失败");
        }

    }

    @ApiOperation(value = "商家类型模块  查询接口",notes = "count:  为 数据的总条数")
    @ApiResponses({
            @ApiResponse(code = 146, message = "查询成功"),
            @ApiResponse(code = 147, message = "查询失败"),
            @ApiResponse(code = 401, message = "未经授权"),
            @ApiResponse(code = 403, message = "被禁止"),
            @ApiResponse(code = 404, message = "请求路径没有货页面跳转路径不对")
    })
    @GetMapping("/")
    public ReturnResult selectShopTypesPage(@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "5") Integer limit){
        try {
            Map map = shopTypeService.selectShopTypesPage(page, limit);
            return ReturnResultUtils.returnSuccess(map);
        } catch (Exception e) {
            log.error("查询失败" + e);
            return ReturnResultUtils.returnFailMsg(147, "查询失败");
        }
    }

    @ApiOperation(value = "展示所有商家类型接口")
    @GetMapping("/showAllShopType")
    public ReturnResult showAllShopType(){
        try{
            List<ShopType> shopTypes = shopTypeService.showAllShopType();
            return ReturnResultUtils.returnSuccess(shopTypes);
        }catch (Exception e) {
            log.error("查询失败" + e);
            return ReturnResultUtils.returnFailMsg(147, "查询失败");
        }
    }
}
